package user

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	//"github.com/gogf/gf-demo-user/v2/internal/model"
	"github.com/gogf/gf-demo-user/v2/internal/model/entity"
	"github.com/gogf/gf-demo-user/v2/api/web/v1"
	"github.com/gogf/gf-demo-user/v2/internal/service"
)

func (c *ControllerV1) Schedule(ctx context.Context, req *v1.ScheduleReq) (res *v1.ScheduleRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var Schedule *entity.Schedule
	err = g.Model("Schedule").Where("Schedule_id=?", req.Schedule_id).Scan(&Schedule)
	fmt.Println(err)
	res = &v1.ScheduleRes{
		Schedule,
	}
	return
}

func (c *ControllerV1) SchedulePost(ctx context.Context, req *v1.SchedulePostReq) (res *v1.SchedulePostRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("Schedule").Data(g.Map{
		"id" : newUUID,
		"start_time" : req.Start_time,
		"end_time" : req.End_time,
		"tag" : req.Title,
		"company" : req.Company,
		"location" : req.Location,
		"link" : req.Link,
		"status" : req.Status,
	}).Insert()
	fmt.Println(id)
	return
}

func (c *ControllerV1) ScheduleDele(ctx context.Context, req *v1.ScheduleDeleReq) (res *v1.ScheduleDeleRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Schedule").Where("Schedule_id=?", account.Id).Delete()
	fmt.Println(id)
	return
}


func (c *ControllerV1) ScheduleUp(ctx context.Context, req *v1.ScheduleUpReq) (res *v1.ScheduleUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Schedule").Data(g.Map{
		"start_time" : req.Start_time,
		"end_time" : req.End_time,
		"tag" : req.Title,
		"company" : req.Company,
		"location" : req.Location,
		"link" : req.Link,
		"status" : req.Status,
	}).Where("id", req.Schedule_id).Update()
	fmt.Println(id)
	return
}




